class StepFlow {
  constructor(max, baseData) {
    this.max = max || 1
    this.step = 1
    this.baseData = baseData
    this.state = {}
  }

  reset() {
    this.step = 1
    this.state = Object.assign({}, this.baseData)
  }

  setState(k, v) {
    this.state[k] = v
    console.log('setState', k, '=', v)
  }

  getState(k, filters) {
    if (filters) {
      return filters(this.state[k])
    } else {
      return this.state[k]
    }
  }

  nextStep() {
    this.step = this.step + 1 > this.max ? this.max : this.step + 1
    console.log('now step is:', this.step)
  }

  lastStep() {
    this.step = this.step - 1 < 1 ? 1 : this.step - 1
    console.log('now step is:', this.step)
  }
}

const flow = new StepFlow(3)

flow.nextStep()
flow.nextStep()
flow.nextStep()
flow.lastStep()
flow.lastStep()
flow.lastStep()

flow.setState('name', 'Luis')
flow.setState('age', 18)
flow.setState('sex', 1)
console.log('直接获取', flow.getState('sex'))
console.log(
  '获取并过滤',
  flow.getState('sex', (v) => (v === 1 ? '男' : '女'))
)
